/*
 * @Author: mikey.zhaopeng
 * @Date:   2016-01-14 16:07:42
 * @Last Modified by:   mikey.zhaopeng
 * @Last Modified time: 2016-07-13 16:53:33
 */

'use strict';

define([
    '../../../common/service'
], function(
    service
) {
    function image() {
        var self = this;
        /**
         * 简单封装 post for _p
         * @private
         * @method _p
         * @param {Object} url
         * @param {Object} params
         * @param {Object} callback
         * @param {Object} errorCallback
         * @return {Object} description
         */
        this._p = function(url, params, callback, errorCallback) {
            kit.loading.show($('body'));
            service.work(url, params, 'POST').then(function(result) {
                if (result.flag == 'success') {
                    if (callback) {
                        callback(result.data);
                    }
                } else if (result.flag == 'fail') {
                    kit.alert(result.errorCode, result.msg);
                    if (errorCallback) {
                        errorCallback(result)
                    }
                }
            }).done(function() {
                kit.loading.hide($('body'));
            });
        }

        /**
         * 查询视频 for queryVideoes
         * @private
         * @method queryImages
         * @param {Object} params
         * @param {Object} callback
         * @param {Object} errorCallback
         * @return {Object} description
         */
        this.queryVideoes = function(params, callback, errorCallback) {
            self._p('/adresource/getAdvideosList', params, function(result) {
                if (callback) {
                    callback(result, params);
                }
            }, errorCallback);
        }

        /**
         * 查询图片总数 for queryImagesCount
         * @private
         * @method queryImagesCount
         * @param {Object} params
         * @param {Object} callback
         * @param {Object} errorCallback
         * @return {Object} description
         */
        this.queryImagesCount = function(params, callback, errorCallback) {
            if (self.imagesCountParams) {
                self.imagesCountParams.cPage = params.cPage;
                if (params.sortList) {
                    self.imagesCountParams.sortList = params.sortList;
                }
                if (_.isEqual(self.imagesCountParams, params)) {
                    return callback(self.imagesCount, params)
                }
            }
            self._p('/adresource/getAdimagesCount', params, function(result) {
                if (callback) {
                    self.imagesCount = result;
                    self.imagesCountParams = $.extend({}, params);
                    self.imagesCountParams.pSize = self.imagesCountParams.pSize + ''; //转换为字符类型
                    return callback(result, params);
                }
            }, errorCallback);
        }

        /**
         ** 得到 app 列表 for queryApps
         * @private
         * @method queryApps
         * @param {Object} callback
         * @param {Object} errorCallback
         * @return {Object} description
         */
        this.queryApps = function(callback, errorCallback) {
            if (self.apps && callback) { //本地做个简单缓存
                return callback(self.apps);
            }
            var params = { //此接口调用基本都是 typeId = 2
                typeId: 2
            }
            self._p('/connectionobjects/searchConnectionObjectsByResourceManage', params, function(result) {
                if (callback) {
                    if (result.length > 0) {
                        result = _.sortBy(result, function(item) { //按照字母做一个排序
                            return item.name.toLowerCase();
                        });
                    }
                    self.apps = result;
                    return callback(result);
                }
            }, errorCallback);
        }

        this.queryLanguage = function(callback, errorCallback) {
            if (self.languages && callback) { //本地做个简单缓存
                return callback(self.languages);
            }
            var params = {
                type: "adlocale",
                locale: I18N.language,
                q: ""
            }
            self._p('/search/searchAdlocale', params, function(result) {
                if (callback) {
                    if (result.length > 0) {
                        result = _.sortBy(result, function(item) { //按照字母做一个排序
                            return item.name.toLowerCase();
                        });
                    }
                    self.languages = result;
                    return callback(result);
                }
            }, errorCallback);
        }



        this.updateAdvideos = function(params, callback, errorCallback) {
            self._p('/adresource/updateAdvideos', params, function(result) {
                if (callback) {
                    return callback(result);
                }
            }, errorCallback);
        }


        this.getAdvideoByIds = function(ids, callback, errorCallback) {
            var params = {
                mongodbIds: ids
            }
            self._p('/adresource/getAdvideoByIds', params, function(result) {
                if (callback) {
                    return callback(result);
                }
            }, errorCallback);
        }
    };
    return new image();
});
